$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/signuprecord/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '真实姓名', name: 'realName', index: 'real_name', width: 80, align: "center"},
            {label: '电话', name: 'phone', index: 'phone', width: 80, align: "center"},
            {label: '身份证', name: 'idcard', index: 'idCard', width: 80, align: "center"},
            {
                label: '状态', name: 'state', index: 'state', width: 80, align: "center", formatter: function (value) {
                    return value === 1 ?
                        '<span class="label label-success">已处理</span>' :
                        '<span class="label label-danger">未处理</span>';
                }
            },
            {label: '科目名称', name: 'subjectName', index: 'subject_name', width: 80, align: "center"},
            {label: '报名时间', name: 'createDate', index: 'create_date', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        signUpRecord: {
            state: "0"
        },
        ruleValidate: {
            realName: [
                {required: true, message: '真实姓名不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ],
            phone: [
                {required: true, message: '电话不能为空', trigger: 'blur'}
            ],
            idcard: [
                {required: true, message: '身份证不能为空', trigger: 'blur'}
            ],
            state: [
                {required: true, message: '状态不能为空', trigger: 'blur'}
            ],
            subjectId: [
                {required: true, message: '科目不能为空', trigger: 'change', type: 'number'}
            ]
        },
        q: {
            name: '',
            subjectId: ''
        },
        allSubjectList: []
    },
    mounted() {
        this.getAllSubjectList();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.signUpRecord = {
                state: "0"
            };
            vm.getAllSubjectList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getAllSubjectList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.signUpRecord.id == null ? "lanling/signuprecord/save" : "lanling/signuprecord/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.signUpRecord),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/signuprecord/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/signuprecord/info/" + id, function (r) {
                vm.signUpRecord = r.signUpRecord;
                vm.signUpRecord.state = vm.signUpRecord.state + '';
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'subjectId': vm.q.subjectId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getAllSubjectList: function () {
            $.get("../../lanling/allsubject/getAllSubjectList", function (r) {
                vm.allSubjectList = r.list;
            });
        },
        stateChange: function (val) {
            vm.signUpRecord.subjectName = val.label.trim();
        }
    }
});